<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Android WIFI功能——WifiManager | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="&amp;emsp;&amp;emsp;之前做了关于Android wifi的相关功能，这里做一个记录">
<meta property="og:type" content="article">
<meta property="og:title" content="Android WIFI功能——WifiManager">
<meta property="og:url" content="https://haichenyi.com/2018/03/05/Android-WIFI功能——WifiManager/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="&amp;emsp;&amp;emsp;之前做了关于Android wifi的相关功能，这里做一个记录">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-03-06T12:59:02.079Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android WIFI功能——WifiManager">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;之前做了关于Android wifi的相关功能，这里做一个记录">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-Android-WIFI功能——WifiManager" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      Android WIFI功能——WifiManager
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2018/03/05/Android-WIFI功能——WifiManager/" class="article-date">
  <time datetime="2018-03-05T12:13:31.000Z" itemprop="datePublished">2018-03-05</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android-常用功能/">Android -常用功能</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <p>&emsp;&emsp;之前做了关于Android wifi的相关功能，这里做一个记录</p>
<a id="more"></a>
<h3 id="第一步：获取WifiManager"><a href="#第一步：获取WifiManager" class="headerlink" title="第一步：获取WifiManager"></a>第一步：获取WifiManager</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">WifiManager wifiMgr = (WifiManager)Context.getSystemService(Context.WIFI_SERVICE);</span><br></pre></td></tr></table></figure></div>
<h3 id="第二步：获取周围热点信息和已连接wifi信息"><a href="#第二步：获取周围热点信息和已连接wifi信息" class="headerlink" title="第二步：获取周围热点信息和已连接wifi信息"></a>第二步：获取周围热点信息和已连接wifi信息</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">List&lt;ScanResult&gt; scanResults = wifiMgr.getScanResults();//扫描到的周围热点信息</span><br><span class="line">WifiInfo info = wifiMgr.getConnectionInfo();//已连接wifi信息</span><br></pre></td></tr></table></figure></div>
<p>这里我说一下几个字段的意思：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">public String name;//ssid对应的wifi名字</span><br><span class="line">public String capabilities;//用来判断是否有密码</span><br><span class="line">public int level;//信号等级，这里是负数</span><br><span class="line">public String BSSID;//macid</span><br></pre></td></tr></table></figure></div>
<p>这里我需要强调的就是level，是负数，<strong><em>负数越大，信号越好</em></strong></p>
<h3 id="第三步：检测wifi信息，这里我给出几个工具类"><a href="#第三步：检测wifi信息，这里我给出几个工具类" class="headerlink" title="第三步：检测wifi信息，这里我给出几个工具类"></a>第三步：检测wifi信息，这里我给出几个工具类</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">   * 连接有密码的wifi.</span><br><span class="line">   *</span><br><span class="line">   * @param SSID     ssid</span><br><span class="line">   * @param Password Password</span><br><span class="line">   * @return apConfig</span><br><span class="line">   */</span><br><span class="line">  private WifiConfiguration setWifiParamsPassword(String SSID, String Password) &#123;</span><br><span class="line">    WifiConfiguration apConfig = new WifiConfiguration();</span><br><span class="line">    apConfig.SSID = &quot;\&quot;&quot; + SSID + &quot;\&quot;&quot;;</span><br><span class="line">    apConfig.preSharedKey = &quot;\&quot;&quot; + Password + &quot;\&quot;&quot;;</span><br><span class="line">    //不广播其SSID的网络</span><br><span class="line">    apConfig.hiddenSSID = true;</span><br><span class="line">    apConfig.status = WifiConfiguration.Status.ENABLED;</span><br><span class="line">    //公认的IEEE 802.11验证算法。</span><br><span class="line">    apConfig.allowedAuthAlgorithms.clear();</span><br><span class="line">    apConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);</span><br><span class="line">    //公认的的公共组密码</span><br><span class="line">    apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);</span><br><span class="line">    apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);</span><br><span class="line">    //公认的密钥管理方案</span><br><span class="line">    apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);</span><br><span class="line">    //密码为WPA。</span><br><span class="line">    apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);</span><br><span class="line">    apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);</span><br><span class="line">    //公认的安全协议。</span><br><span class="line">    apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);</span><br><span class="line">    return apConfig;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 连接没有密码wifi.</span><br><span class="line">   *</span><br><span class="line">   * @param ssid ssid</span><br><span class="line">   * @return configuration</span><br><span class="line">   */</span><br><span class="line">  private WifiConfiguration setWifiParamsNoPassword(String ssid) &#123;</span><br><span class="line">    WifiConfiguration configuration = new WifiConfiguration();</span><br><span class="line">    configuration.SSID = &quot;\&quot;&quot; + ssid + &quot;\&quot;&quot;;</span><br><span class="line">    configuration.status = WifiConfiguration.Status.ENABLED;</span><br><span class="line">    configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);</span><br><span class="line">    configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);</span><br><span class="line">    configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);</span><br><span class="line">    configuration.allowedPairwiseCiphers</span><br><span class="line">        .set(WifiConfiguration.PairwiseCipher.TKIP);</span><br><span class="line">    configuration.allowedPairwiseCiphers</span><br><span class="line">        .set(WifiConfiguration.PairwiseCipher.CCMP);</span><br><span class="line">    configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);</span><br><span class="line">    return configuration;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  public static final int WIFI_NO_PASS = 0;</span><br><span class="line">  private static final int WIFI_WEP = 1;</span><br><span class="line">  private static final int WIFI_PSK = 2;</span><br><span class="line">  private static final int WIFI_EAP = 3;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 判断是否有密码.</span><br><span class="line">   *</span><br><span class="line">   * @param result ScanResult</span><br><span class="line">   * @return 0</span><br><span class="line">   */</span><br><span class="line">  public static int getSecurity(MyScanResult result) &#123;</span><br><span class="line">    if (null != result &amp;&amp; null != result.capabilities) &#123;</span><br><span class="line">      if (result.capabilities.contains(&quot;WEP&quot;)) &#123;</span><br><span class="line">        return WIFI_WEP;</span><br><span class="line">      &#125; else if (result.capabilities.contains(&quot;PSK&quot;)) &#123;</span><br><span class="line">        return WIFI_PSK;</span><br><span class="line">      &#125; else if (result.capabilities.contains(&quot;EAP&quot;)) &#123;</span><br><span class="line">        return WIFI_EAP;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return WIFI_NO_PASS;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div>
<p>上面三个工具类，一个用来判断是否需要密码，另外两个用来连接wifi</p>
<h3 id="连接wifi"><a href="#连接wifi" class="headerlink" title="连接wifi"></a>连接wifi</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//需要密码</span><br><span class="line">wifiMgr.enableNetwork(wifiMgr.addNetwork(</span><br><span class="line">                    setWifiParamsPassword(mClickResult.name, password)), true);</span><br><span class="line">//不需要密码</span><br><span class="line">wifiMgr.enableNetwork(wifiMgr.addNetwork(setWifiParamsNoPassword(mClickResult.name)),</span><br><span class="line">              true);</span><br></pre></td></tr></table></figure></div>
<h3 id="wifi监听"><a href="#wifi监听" class="headerlink" title="wifi监听"></a>wifi监听</h3><p>PS：关于wifi状态的监听，就是监听你当前的网络状态即可。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;receiver android:name=&quot;.receiver.NetworkReceiver&quot;&gt;</span><br><span class="line">            &lt;intent-filter&gt;</span><br><span class="line">                &lt;action android:name=&quot;android.net.wifi.WIFI_STATE_CHANGED&quot;/&gt;</span><br><span class="line">                &lt;action android:name=&quot;android.net.wifi.STATE_CHANGE&quot;/&gt;</span><br><span class="line">                &lt;action android:name=&quot;android.net.wifi.supplicant.STATE_CHANGE&quot;/&gt;</span><br><span class="line">                &lt;action android:name=&quot;android.net.wifi.NETWORK_IDS_CHANGED&quot;/&gt;</span><br><span class="line">            &lt;/intent-filter&gt;</span><br><span class="line">        &lt;/receiver&gt;</span><br></pre></td></tr></table></figure></div>
<p>首先在你的清单文件注册这四种action的广播，这里给出我的网络监听广播</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2018/1/10</span><br><span class="line"> * Desc: 网络广播监听</span><br><span class="line"> */</span><br><span class="line">public class NetworkReceiver extends BroadcastReceiver &#123;</span><br><span class="line">  public static final int STATE1 = 1;//密码错误</span><br><span class="line">  public static final int STATE2 = 2;//连接成功</span><br><span class="line">  public static final int STATE3 = 3;//连接失败</span><br><span class="line">  public static final int STATE4 = 4;//正在获取ip地址</span><br><span class="line">  public static final int STATE5 = 5;//正在连接</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onReceive(Context context, Intent intent) &#123;</span><br><span class="line">    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) &#123;</span><br><span class="line">      //密码错误广播,是不是正在获得IP地址</span><br><span class="line">      int linkWifiResult = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);</span><br><span class="line">      if (linkWifiResult == WifiManager.ERROR_AUTHENTICATING) &#123;</span><br><span class="line">        //密码错误</span><br><span class="line">        ToastUtils.showTipMsg(R.string.password_wrong);</span><br><span class="line">        sendNetworkStateChange(new NetWorkInfo().setState(STATE1));</span><br><span class="line">      &#125;</span><br><span class="line">      SupplicantState supplicantState = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);</span><br><span class="line">      NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(supplicantState);</span><br><span class="line">      Log.v(Constants.HTTP_WZ, state.name());</span><br><span class="line">      if (state == NetworkInfo.DetailedState.CONNECTING) &#123;</span><br><span class="line">        //正在连接</span><br><span class="line">        ToastUtils.showTipMsg(R.string.linking);</span><br><span class="line">        sendNetworkStateChange(new NetWorkInfo().setState(STATE5));</span><br><span class="line">      &#125; else if (state == NetworkInfo.DetailedState.FAILED</span><br><span class="line">          || state == NetworkInfo.DetailedState.DISCONNECTING) &#123;</span><br><span class="line">        //连接失败</span><br><span class="line">        sendNetworkStateChange(new NetWorkInfo().setState(STATE3));</span><br><span class="line">        ToastUtils.showTipMsg(R.string.linked_failed);</span><br><span class="line">      &#125; else if (state == NetworkInfo.DetailedState.CONNECTED) &#123;</span><br><span class="line">        //连接成功</span><br><span class="line">        ToastUtils.showTipMsg(R.string.linked_success);</span><br><span class="line">      &#125; else if (state == NetworkInfo.DetailedState.OBTAINING_IPADDR) &#123;</span><br><span class="line">        //正在获取ip地址</span><br><span class="line">        sendNetworkStateChange(new NetWorkInfo().setState(STATE4));</span><br><span class="line">      &#125; else if (state == NetworkInfo.DetailedState.IDLE) &#123;</span><br><span class="line">        //闲置的</span><br><span class="line">        ConnectivityManager connectManager = (ConnectivityManager) BaseApplication.getInstance()</span><br><span class="line">            .getSystemService(Context.CONNECTIVITY_SERVICE);</span><br><span class="line">        NetworkInfo activeNetworkInfo = null;</span><br><span class="line">        if (connectManager != null) &#123;</span><br><span class="line">          activeNetworkInfo = connectManager.getActiveNetworkInfo();</span><br><span class="line">        &#125;</span><br><span class="line">        if (activeNetworkInfo == null) &#123;</span><br><span class="line">          sendNetworkStateChange(new NetWorkInfo().setState(STATE3));</span><br><span class="line">          ToastUtils.showTipMsg(R.string.linked_failed);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) &#123;</span><br><span class="line">      // 监听wifi的打开与关闭，与wifi的连接无关</span><br><span class="line">      int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);</span><br><span class="line">      Log.v(Constants.HTTP_WZ, &quot;wifiState:&quot; + wifiState);</span><br><span class="line">      switch (wifiState) &#123;</span><br><span class="line">        case WifiManager.WIFI_STATE_DISABLING://正在停止0</span><br><span class="line">          ToastUtils.showTipMsg(R.string.close_wifi);</span><br><span class="line">          break;</span><br><span class="line">        case WifiManager.WIFI_STATE_DISABLED://已停止1</span><br><span class="line">          break;</span><br><span class="line">        case WifiManager.WIFI_STATE_UNKNOWN://未知4</span><br><span class="line">          break;</span><br><span class="line">        case WifiManager.WIFI_STATE_ENABLING://正在打开2</span><br><span class="line">          ToastUtils.showTipMsg(R.string.opening_wifi);</span><br><span class="line">          break;</span><br><span class="line">        case WifiManager.WIFI_STATE_ENABLED://已开启3</span><br><span class="line">          break;</span><br><span class="line">        default:</span><br><span class="line">          break;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) &#123;</span><br><span class="line">      // 监听wifi的连接状态即是否连上了一个有效无线路由</span><br><span class="line">      Parcelable parcelableExtra = intent</span><br><span class="line">          .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);</span><br><span class="line">      if (null != parcelableExtra) &#123;</span><br><span class="line">        // 获取联网状态的NetWorkInfo对象</span><br><span class="line">        NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;</span><br><span class="line">        //获取的State对象则代表着连接成功与否等状态</span><br><span class="line">        NetworkInfo.State state = networkInfo.getState();</span><br><span class="line">        //判断网络是否已经连接</span><br><span class="line">        boolean isConnected = state == NetworkInfo.State.CONNECTED;</span><br><span class="line">        Log.v(Constants.HTTP_WZ, &quot;isConnected:&quot; + isConnected);</span><br><span class="line">        if (isConnected) &#123;</span><br><span class="line">          ToastUtils.showTipMsg(R.string.linked_success);</span><br><span class="line">          sendNetworkStateChange(new NetWorkInfo().setState(STATE2));</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 发送网络状态eventBus.</span><br><span class="line">   *</span><br><span class="line">   * @param info info</span><br><span class="line">   */</span><br><span class="line">  private void sendNetworkStateChange(NetWorkInfo info) &#123;</span><br><span class="line">    EventBus.getDefault().post(info);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(&quot;unused&quot;)</span><br><span class="line">  private String getConnectionType(int type) &#123;</span><br><span class="line">    String connType = &quot;&quot;;</span><br><span class="line">    if (type == ConnectivityManager.TYPE_MOBILE) &#123;</span><br><span class="line">      connType = &quot;移动网络&quot;;</span><br><span class="line">    &#125; else if (type == ConnectivityManager.TYPE_WIFI) &#123;</span><br><span class="line">      connType = &quot;WIFI网络&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">    return connType;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public static class NetWorkInfo &#123;</span><br><span class="line">    public int state;//1密码错误，2:连接成功，3:连接失败</span><br><span class="line"></span><br><span class="line">    private NetWorkInfo setState(int state) &#123;</span><br><span class="line">      this.state = state;</span><br><span class="line">      return this;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>注释应该写的很清楚，我就不做过多的解释了。</p>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2018/03/05/Android二维码扫描——BGAQRCode-Android/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            Android二维码扫描——BGAQRCode-Android
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2018/03/05/简单的蓝牙通信/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">简单的蓝牙通信</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>